package Server;

import java.sql.*;
import java.util.*; 

public class DbConnect {
	private Connection dbconn;
	private Statement dbstate;
	private ResultSet dbresult;
	
	public DbConnect(){
		dbconn = null;
		dbstate = null;
		dbresult = null;
	}
	
	public void print(String str){
		System.out.println(str);
	}
	
	public boolean  dbConnect(String host,String port, String dbName, String username, String password){
		String driverName = "com.mysql.jdbc.Driver";
		String dbHost = host;
		String dbPort = port;
		String enCoding = "?userUnicode=true&characterEncoding=gbk";
		String userName = username;
		String pwd = password;
		String url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + enCoding; 
		try 
		{ 
			Class.forName(driverName).newInstance(); 
			dbconn = DriverManager.getConnection(url, userName, pwd); 
		}catch(Exception e){ 
			print("Exception: " + e.getMessage());
		} 
		return dbconn!=null ? true : false;
	}
	
	
	public boolean dbInsert(String tabName, HashMap values) 
	{ 
		String sql = ""; 
		String insertFields = "", temFields = ""; 
		String insertValues = "", temValues = ""; 
		boolean insertResult = false; 
		Iterator keyValues = values.entrySet().iterator(); 
		for(int i = 0; i<values.size(); ++i) 
		{ 
			Map.Entry entry = (Map.Entry) keyValues.next(); 
			Object key = entry.getKey(); 
			Object value = entry.getValue(); 
			temFields += key + ", "; 
			temValues += "'" + value + "'" + ", "; 
		} 
		insertFields = temFields.substring(0, temFields.length()-2); 
		insertValues = temValues.substring(0, temValues.length()-2); 
		sql += "insert into " + tabName + " (" + insertFields + ") values" + "(" + insertValues + ")"; 
		try 
		{ 
			dbstate = dbconn.createStatement(); 
			dbstate.executeUpdate(sql); 
			insertResult = true; 
		}catch(Exception e){ 
			print("Sql = " + sql); 
			print("Exception: " + e.getMessage()); 
		} 
		return insertResult ? true : false;
	}
	
	public ResultSet dbSelect(String tableName, ArrayList fields, String selCondition) 
	{ 
		ArrayList mapInList = new ArrayList();	 
		String selFields = ""; 
		for (int i = 0; i<fields.size(); ++i) 
			selFields += fields.get(i) + ", "; 
		String selFieldsTem = selFields.substring(0, selFields.length()-2);//鏍规嵁String鐨勭储寮曟彁鍙栧瓙涓�
		try{ 
			dbstate = dbconn.createStatement(); 
			ResultSet dbrst = null;
			String sql = "select " + selFieldsTem + " from " + tableName +" "+ selCondition; 
			//print("sql = " + sql); 
			try{ 
				dbrst = dbstate.executeQuery(sql); 
			}catch(Exception err){ 
				print("Sql = " + sql); 
				print("Exception: " + err.getMessage()); 
			} 
			return dbrst; 
		}catch(Exception e){ 
			print("Exception: " + e.getMessage()); 
		} 
		return null; 
	}//end String dbSelect(鈥� 
	
	
	public static void main(String [] args){
		
		DbConnect db = new DbConnect();
		if(db.dbConnect("localhost", "3312", "tank", "root", "root")){
			ArrayList fields = new ArrayList();
			fields.add("id");
			fields.add("word");
			fields.add("chinese");
			//ResultSet rst = db.dbSelect("words", fields, "where word like 'ab%'");

		}else{
			System.out.println("error");
		}
	}
}
